<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 项目数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\VersionService;
use common\validate\CosMedia;
use common\validate\Media;
use common\validate\Version;
use support\ErrorCode;
use think\Exception;
use think\Request;
use think\Response;

class VersionController extends VerifyController
{
    /**
     * @var VersionService
     */
    private $versionService;

    public function __construct()
    {
        parent::__construct();
        $this->versionService = VersionService::getInstance();
    }

    /**
     * 获得一个任务版本
     * @param Request $request
     * @return \think\Response
     */
    public function getTaskVersionForReviewRoom(Request $request)
    {
        $param = $request->param();
        $res = $this->versionService->getTaskVersionForReviewRoom($param['param'], $request->getUserId());
        return json(success_response('', $res));
    }

    /**
     * 提交版本
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function create(Request $request)
    {
        $param = $request->param();

        $mediaType = 'media';
        $validate = new Version();
        if (empty($param['data']['media']) && !empty($param['data']['cos_media'])) {
            $mediaType = 'cos_media';
            $validate->scene('CreateCosMedia');
            $mediaValidate = new CosMedia();
            $mediaValidate->scene('CreateCosMedia');
        } else {
            $validate->scene('CreateMedia');
            $mediaValidate = new Media();
            $mediaValidate->scene('CreateMedia');
        }
        if (!$validate->check($param)) {
            throw_strack_exception($validate->getError(), -1);
        }

        foreach ($param['data'][$mediaType] as $media) {
            if (!$mediaValidate->check($media)) {
                throw_strack_exception($mediaValidate->getError(), ErrorCode::CREATE_MEDIA_PARAM);
            }
        }

        try {
            $res = $this->versionService->create($param['data'], $mediaType);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查询任务版本列表
     * @param Request $request
     * @return \think\Response
     */
    public function selectTaskVersions(Request $request)
    {
        $param = $request->param();
        $res = $this->versionService->selectTaskVersions($param['param']);
        return json(success_response('', $res));
    }

    /**
     * 查询版本关联媒体列表
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function selectVersionMediaList(Request $request)
    {
        $param = $request->param();
        $res = $this->versionService->selectVersionMediaList($param['param'], $request->getModuleCode());
        return json(success_response('', $res));
    }

    /**
     * 查询任务最后一个版本的提交 审核未通过信息
     * @param Request $request
     * @return \think\Response
     */
    public function getTaskLatestVersionFailureReviewResult(Request $request)
    {
        $param = $request->param();
        $res = $this->versionService->getTaskLatestVersionFailureReviewResult($param['param'], $request->getTenantId());
        return json(success_response("OK", $res));
    }

    /**
     * 查询任务所属实体的动态信息
     * @param Request $request
     * @return \think\Response
     */
    public function getTaskEntityActivity(Request $request)
    {
        $param = $request->param();
        if (!isset($param['param']['page'])) {
            $param['param']['page'] = [1, C("database.database_max_select_rows")];
        }

        if (!isset($param['param']['filter']['step_id'])) {
            $param['param']['filter']['step_id'] = 0;
        }

        $resData = $this->versionService->getTaskEntityLevelActivity($param['param']['filter']['id'], $param['param']['page'], $param['param']['filter']['step_id']);
        return json(success_response("", $resData));
    }

    /**
     * 查询任务关联动态工序筛选列表
     * @param Request $request
     * @return \think\Response
     */
    public function getTaskEntityActivityStepFilter(Request $request)
    {
        $param = $request->param();
        $resData = $this->versionService->getTaskEntityActivityStepFilter($param['param']['filter']['id']);
        return json(success_response("", $resData));
    }

    /**
     * @param Request $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        return parent::update($request);
    }


    /**
     * 获取任务最大版本列表
     * @param Request $request
     * @return Response
     */
    public function getTaskMaxVersionList(Request $request)
    {
        $param = $request->param()["param"];
        $resData = $this->versionService->getTaskMaxVersionList($param);
        return json(success_response("", $resData));
    }

    /**
     * 获取没有上传的统计
     * @param Request $request
     * @return Response
     */
    public function getNoUploadTaskMaxVersionCount(Request $request)
    {
        $param = $request->param()["param"];
        $resData = $this->versionService->getNoUploadTaskMaxVersionCount($param);
        return json(success_response("", $resData));
    }

}
